package com.library.controller;

import com.library.bean.Book;
import com.library.bean.Lend;
import com.library.bean.LendDate;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@Controller
@CrossOrigin
public class LendController {

    @Autowired
    private LendService lendService;
    @Autowired
    private BookService bookService;

    @RequestMapping("/lendlist.html")
    public ModelAndView lendlist(){
        ModelAndView modelAndView=new ModelAndView("admin_lend_list");

        ArrayList<Lend> lends=lendService.lendlist();

        ArrayList<LendDate> lendDates=new ArrayList<LendDate>();

        //将lend对象中的日期格式化
        for(Lend lend:lends){
            LendDate lendDate=new LendDate();
            //将lend对象中的属性值赋值给lengDate对象;
            BeanUtils.copyProperties(lend,lendDate);
            if (lend.getLend_date()!=null){
                lendDate.setLendDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getLend_date()));
            }
            if (lend.getBack_date()!=null){
                lendDate.setBackDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getBack_date()));
            }
            lendDates.add(lendDate);

        }
        modelAndView.addObject("list",lendDates);
        return modelAndView;

    }

    @RequestMapping("/lendbook.html")
    public String lendBook(HttpServletRequest request, RedirectAttributes redirectAttributes){
        long bookId=Long.parseLong(request.getParameter("bookId"));
        long readId=((ReaderCard)request.getSession().getAttribute("readercard")).getReader_id();
        if (lendService.lendBook(bookId,readId)){
            redirectAttributes.addFlashAttribute("succ","借书成功");
        }else redirectAttributes.addFlashAttribute("error","借书失败");
        return "redirect:/admin_books.html";

    }


    @RequestMapping("/returnbook.html")
    public String returnbook(HttpServletRequest request,RedirectAttributes redirectAttributes){
        long bookId=Long.parseLong(request.getParameter("bookId"));
        long readId=((ReaderCard)request.getSession().getAttribute("readercard")).getReader_id();
        if (lendService.returnBook(bookId,readId)){
            redirectAttributes.addFlashAttribute("succ","归还成功");
        }else redirectAttributes.addFlashAttribute("error","归还失败");
        return "redirect:/admin_books.html";

    }


    @RequestMapping("/lendbooknum.html")
    public String bookLendNum(HttpServletRequest request,RedirectAttributes redirectAttributes){
        long bookId=Long.parseLong(request.getParameter("bookId"));

        Book book=bookService.getBookById(bookId);

        if(bookService.updatebooknum(book)){
            redirectAttributes.addFlashAttribute("succ","借阅成功");
        }else {
            redirectAttributes.addFlashAttribute("error","借阅失败");
        }
        return "redirect:/admin_books.html";

    }

    @RequestMapping("/returnbooknum.html")
    public String returnbooknum(HttpServletRequest request,RedirectAttributes redirectAttributes){
        long bookId=Long.parseLong(request.getParameter("bookId"));

        Book book=bookService.getBookById(bookId);

        if(bookService.returnbooknum(book)){
            redirectAttributes.addFlashAttribute("succ","还书成功");
        }else {
            redirectAttributes.addFlashAttribute("error","还书失败");
        }
        return "redirect:/admin_books.html";

    }

    @RequestMapping("mylend.html")
    public ModelAndView mylend(HttpServletRequest request){
        ReaderCard readerCard= (ReaderCard) request.getSession().getAttribute("readercard");
        ModelAndView modelAndView=new ModelAndView("reader_lend_list");
        ArrayList<Lend> lends=lendService.myLendList(readerCard.getReader_id());

        ArrayList<LendDate> lendDates=new ArrayList<LendDate>();

        //将lend对象中的日期格式化
        for(Lend lend:lends){
            LendDate lendDate=new LendDate();
            //将lend对象中的属性值赋值给lengDate对象;
            BeanUtils.copyProperties(lend,lendDate);
            if (lend.getLend_date()!=null){
                lendDate.setLendDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getLend_date()));
            }
            if (lend.getBack_date()!=null){
                lendDate.setBackDateStr(new SimpleDateFormat("yyyy-MM-dd").format(lend.getBack_date()));
            }
            lendDates.add(lendDate);

        }
        modelAndView.addObject("list",lendDates);
        return modelAndView;


    }





}
